﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery popup
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.modal win
2.dialog
3.message：alert,show,confirm
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    ///#region modalwin
    $.modalWin = function (options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        ///#region 属性 
        var options = $.extend({
            id: iconfig.modal.contentid
            , title: $iconst.controls.modal.title
            , width: iconfig.modal.width
            , height: iconfig.modal.height
            , zindex: iconfig.modal.zindex
            , allowmove: true
            , css: iconfig.modal.css
            , url: null
            , fnclose: null
        }, options);
        ///#endregion

        var win = $("#" + options.id);
        if (win.length == 0)
            win = setContent($(document.createElement("div")), options).attr("id", options.id);
        //弹出封闭页面的modal
        var modalpanel = buildModal(options.id, 'modalwin', options, 'win');
        ictrcore.setCenter(modalpanel);
        ictrcore.modal('win');
        modalpanel.append(win).fadeIn('slow');

        ictrcore.loadPage(options.url, win);
    }

    $.fn.dialog = function (options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>

        ///#region 属性 
        var options = $.extend({
            title: $iconst.controls.modal.title
            , width: iconfig.dialog.width
            , height: iconfig.dialog.height
            , zindex: iconfig.dialog.zindex
            , allowmove: true
            , css: iconfig.modal.css
            , fnclose: null
        }, options);
        ///#endregion

        var content = $(this);
        var id = content.attr('id');
        var dialog = $('#' + id + '_dialog');

        if (dialog.length == 0) {
            content.show();
            content = setContent(content, options);
            dialog = buildModal(id, id + '_dialog', options, 'dialog');
            ictrcore.setCenter(dialog);
            content.appendTo(dialog);
        }
        ictrcore.modal('dialog');
        dialog.fadeIn('slow');

    };

    function buildModal(id, modalid, options, type, addclose) {

        var div = $(document.createElement('div'));

        var modalpanel = $('#' + modalid);
        if (modalpanel.length == 0)
            modalpanel = div.clone().addClass(options.css).attr('id', modalid).appendTo(document.body);
        modalpanel.css({ 'width': options.width + 4, 'height': (options.height ? options.height + 24 : 'auto'), 'z-index': options.zindex })

        ///#region add head
        var head = div.clone().addClass(options.css + '-head');
        //add head title
        var title = div.clone().addClass(options.css + '-title').text(options.title).appendTo(head);
        //add head btn
        if (!hasValue(addclose) || addclose) {
            var fnclose = options.fnclose;
            var close = $(document.createElement('a')).addClass(options.css + '-close').click(function () {
                $.modalPanelClose(type,id);
                if (fnclose)
                    fnclose();
            });
            head.append(div.addClass(options.css + '-tool').append(close));
        }

        modalpanel.append(head);
        if (options.allowmove)
            title.drag({ handler: '#' + modalid });
        ///#endregion
        return modalpanel;
    }
    function setContent(content, options) {
        return content.addClass(options.css + '-content').css({ 'width': options.width, 'height': options.height });
    }

    $.modalPanelClose = function (type,id) {
        //$('#' + id + '_modalpanel')[0].innerHTML = '';
        switch (type) {
            case "win":
                $('#modalwin').html('').empty().hide();
                break;
            case "dialog":
                $('#' + id + '_dialog').hide();
                break;
            case "msg":
            case "show":
                $('#' + (id || 'modalmessage')).html('').empty().hide();
                break;
        }
        if (type != 'show')
            ictrcore.modalClose(type);
    }

    $.message = function (msg, options) {
        var title;
        if (!options)
            options = { type: 'alert' };
        if (options.type === 'confirm')
            title = $iconst.controls.message.confirm;
        else {
            title = getJsonValue($iconst.controls.message, options.showtype);
            if (!title)
                title = $iconst.controls.message.normal;
        }

        var id = options.type === 'show' ? 'msg_' + parseInt(Math.random() * 10000) : 'modalmessage';
        var settings = iconfig.message;
        settings.title = title;
        var message = setContent($(document.createElement("div")), settings).html(msg);

        var modalpanel = buildModal(null, id, settings, 'msg', options.type === 'show').append(message);
        if (options.type === 'show') {
            ictrcore.setShow(modalpanel);
            modalpanel.fadeIn('slow');
            setTimeout(function () { $.modalPanelClose('show',id); }, options.timeout || settings.timeout);
        }
        else {
            var btnpanel = $(document.createElement('div')).addClass('btns'), fnok = options.fnok,
            btnok = $.button(null, { name: 'ok' }).click(function () { if (fnok) fnok(); $.modalPanelClose('msg'); }); ;

            btnpanel.append(btnok).appendTo(modalpanel);
            if (options.type === 'confirm') {
                var fncancel = options.fncancel,
                btncanel = $.button(null, { name: 'cancel' })
                .click(function () { if (fncancel) fncancel(); $.modalPanelClose('msg'); }).appendTo(btnpanel);
            }
            ictrcore.setCenter(modalpanel);
            ictrcore.modal('msg');
            modalpanel.fadeIn('fast');
        }

    }
})(jQuery);